package com.valeandre.euroro;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.valeandre.euroro.adapter.MaterialsAdapter;
import com.valeandre.euroro.model.Currency;
import com.valeandre.euroro.model.Price;
import com.valeandre.euroro.model.Prices;
import com.valeandre.euroro.model.XmlGold;
import com.valeandre.euroro.net.NetworkService;
import com.valeandre.euroro.net.NetworkServiceListener;

public class PreciousMetalsActivity extends Activity {

	public final static String EXTRA_MESSAGE = "com.valeandre.euroro.value";
	public final static String GOLD_EXTRA = "com.valeandre.euroro.goldvalue";
	public final static String SILVER_EXTRA = "com.valeandre.euroro.silvervalue";
	public final static String PLATINUM_EXTRA = "com.valeandre.euroro.platinumvalue";
	public final static String PALLADIUM_EXTRA = "com.valeandre.euroro.palladiumvalue";
	public Prices mxmlgold;
	public String mvaluta = "eur";
	public double mgoldconversion;
	public double msilverconversion;
	public double mplatinumconversion;
	public double mpalladiumconversion;
	private ProgressDialog mProgressDialog;
	Handler mHandler= new Handler();
	final Context context = this;
	private MaterialsAdapter mAdapter;
	private ListView mList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_precious_metals);
		mList = (ListView) findViewById(R.id.list);
		mAdapter = new MaterialsAdapter(new Currency(), this);
		mList.setAdapter(mAdapter);
		
		Button plotButton = (Button) findViewById(R.id.button_plot);

		final Button currencyButton = (Button) findViewById(R.id.edit_message);

		// add button listener
		currencyButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {

				// custom dialog
				final Dialog dialog = new Dialog(context);
				dialog.setContentView(R.layout.custom);
				dialog.setTitle("Select currency...");

				// set the custom dialog components - text, image and button
				final TextView text = (TextView) dialog.findViewById(R.id.eur);
				text.setText("EUR");
				text.setOnClickListener(new OnClickListener(){

					public void onClick(View arg0) {
						mvaluta="eur";
						mHandler.post(new Runnable() {

							public void run() {
								currencyButton.setText("EUR");
								goldConversion(null);
							}
						});
						dialog.dismiss();

					}

				});
				final TextView text1 = (TextView) dialog.findViewById(R.id.usd);
				text1.setText("USD");
				text1.setOnClickListener(new OnClickListener(){

					public void onClick(View arg0) {
						mvaluta="usd";
						mHandler.post(new Runnable() {

							public void run() {
								currencyButton.setText("USD");
								goldConversion(null);
							}
						});
						dialog.dismiss();


					}

				});
				final TextView text2 = (TextView) dialog.findViewById(R.id.chf);
				text2.setText("CHF");
				text2.setOnClickListener(new OnClickListener(){

					public void onClick(View arg0) {
						mvaluta="chf";
						mHandler.post(new Runnable() {

							public void run() {
								currencyButton.setText("CHF");
								goldConversion(null);
							}
						});
						dialog.dismiss();


					}

				});

				dialog.show();
			}

		});
		
		goldConversion(null);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	/** Called when the user clicks the Send button */
	public void goldConversion(View view) {
		//	EditText editText = (EditText) findViewById(R.id.edit_message);
		showProgress();
		getConversion(mvaluta);
		//    	Intent intent = new Intent(this, DisplayConversionActivity.class);
		//    	
		//    	Double value=0.0; //get the response of the feed
		//    	intent.putExtra(EXTRA_MESSAGE, value);
		//    	startActivity(intent);
	}

	/** Called when the user clicks the Show_plot button */
	public void showPlot(View view) {
		//	EditText editText = (EditText) findViewById(R.id.edit_message);
		//showProgress();

		if(mgoldconversion!=0&&msilverconversion!=0&&mplatinumconversion!=0){
			Intent intent = new Intent(this, DisplayPlotActivity.class);

			intent.putExtra(GOLD_EXTRA, mgoldconversion);
			intent.putExtra(SILVER_EXTRA, msilverconversion);
			intent.putExtra(PLATINUM_EXTRA, mplatinumconversion);
			intent.putExtra(EXTRA_MESSAGE, mvaluta);
			intent.putExtra(PALLADIUM_EXTRA, mpalladiumconversion);
			startActivity(intent);
		}
	}




	public void getConversion(final String valuta) {
		Log.d("EURORO", "getConversion");

		NetworkService.getInstance().getConversionValues(new NetworkServiceListener() {

			@Override
			public void conversionPerformed(final Prices xml) {
				Log.d("EURORO", "goldConversionPerformed");
				mxmlgold=xml;
				mHandler.post(new Runnable() {

					public void run() {
//						showConversion(valuta);
						mAdapter.setData(xml.getCurrency(valuta));
					}
				});
				hideProgress();
			}

			@Override
			public void conversionFailed(int statusCode) {
				Log.e("EURORO", "goldConversionFailed " + statusCode);
				mgoldconversion=0;
				msilverconversion=0;
				mplatinumconversion=0;
				mpalladiumconversion=0;
				hideProgress();
				//				showConnectionAlertDialogAsync();
			}

			@Override
			public void conversionFailed(Exception e) {
				Log.e("EURORO", "goldConversionFailed " + e.getMessage());
				mgoldconversion=0;
				msilverconversion=0;
				mplatinumconversion=0;
				mpalladiumconversion=0;
				hideProgress();
				//				showConnectionAlertDialogAsync();
			}
		});

	}


	public void showConversion(String valuta){

		hideProgress();

		Currency currency = mxmlgold.getCurrency(mvaluta);
		if(currency != null) {
			mgoldconversion = currency.getPrice(Price.GOLD).getValue();
			msilverconversion = currency.getPrice(Price.SILVER).getValue();
			mplatinumconversion = currency.getPrice(Price.PLATINUM).getValue();
			mpalladiumconversion = currency.getPrice(Price.PALLADIUM).getValue();
		}
		 
		DecimalFormat df = new DecimalFormat("#.##");
		
		TextView conversion = (TextView) findViewById(R.id.conversion_value);
//		CharSequence c= String.valueOf(mgoldconversion);
		conversion.setText(df.format(mgoldconversion));

		TextView conversionsilver = (TextView) findViewById(R.id.conversionsilver_value);
//		CharSequence csilver= String.valueOf(msilverconversion);
		conversionsilver.setText(df.format(msilverconversion));

		TextView conversionplatinum = (TextView) findViewById(R.id.conversionplatinum_value);
//		CharSequence cplatinum= String.valueOf(mplatinumconversion);
		conversionplatinum.setText(df.format(mplatinumconversion));
		
		TextView conversionpalladium = (TextView) findViewById(R.id.conversionpalladium_value);
//		CharSequence cpalladium= String.valueOf(mpalladiumconversion);
		conversionpalladium.setText(df.format(mpalladiumconversion));
	}


	public void showProgress() {
		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog == null) {
					mProgressDialog = ProgressDialog.show(PreciousMetalsActivity.this, "Attendere", "Attempting to http://www.xmlcharts.com/precious-metals.html...", true, false);
				}

			}
		});


	}

	public void hideProgress() {
		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog != null && mProgressDialog.isShowing()) {
					mProgressDialog.dismiss();
					mProgressDialog = null;
				}

			}
		});


	}

}
